package com.ease.map.control.basemaptool {
    import com.ease.map.Map;
    import com.ease.map.common.enum.HandlerEnum;
    import com.ease.map.common.enum.LayerTypeEnum;
    import com.ease.map.common.util.GMath;
    import com.ease.map.common.util.ResourceUtil;
    import com.ease.map.constant.LayerIdConstants;
    import com.ease.map.constant.StringConstants;
    import com.ease.map.control.Blackboard;
    import com.ease.map.events.DrawEvent;
    import com.ease.map.feature.Feature;
    import com.ease.map.geometry.basetype.Location;
    import com.ease.map.handler.feature.draw.DrawHandler;
    import com.ease.map.handler.feature.draw.DrawLineHandler;
    import com.ease.map.layer.FeatureLayer;
    import com.kgis.map.projection.Projection;
    import com.ease.map.style.PolylineStyle;

    import mx.collections.ArrayCollection;
    import mx.controls.Alert;
    import mx.controls.Image;
    import com.ease.map.control.basemaptool.AbstractBaseMapTool;

    /**
     * 测量距离工具类
     * */
    [ResourceBundle("MapCore")]
    public class MeasurePathControl extends AbstractBaseMapTool {
        private var board:Blackboard;
        private var count:int;
        private var points:ArrayCollection;
        private var previousLoc:Location;
        private var totalLength:int;
        private var currentLinLength:int;
        private var info:String;
        private var handler:DrawLineHandler;
        private var layer:FeatureLayer;

        public function MeasurePathControl(value:Map) {
            super(value);
        }

        override public function execute():void {
            layer = new FeatureLayer(LayerIdConstants.MEASUREPATH_LAYERID, "measurelength");
            layer.layerType = LayerTypeEnum.DYNAMICLAYER;
            var style:PolylineStyle = new PolylineStyle();

            style.color = 0xFF0000;
            style.width = [3];
            style.alpha = 1;
            layer.geoStyle = style;
            this._map.addLayer(layer, true);

            handler = this._map.getHandler(HandlerEnum.DRAW_LINE_HANDLER) as DrawLineHandler;
            handler.active = true;
            handler.drawLayer = layer;
            this.map.addEventListener(DrawEvent.DRAW_NEW_POINT, drawPointHandler);
            this.map.addEventListener(DrawEvent.DRAW_LINE_FEATURE_END, drawEndHandler)

            board = this._map.getControlByName("blackBoard") as Blackboard;
            board.visible = true;
            board.initialize();
            count = 0;
            totalLength = 0;
            points = new ArrayCollection();
            info = ResourceUtil.getString(StringConstants.MEASUREPATHCONTROL_INFO_START);
            board.setInfo(info);
        }

        private function drawPointHandler(ev:DrawEvent):void {
            //画第一个点
            previousLoc = ev.newPoint;
            points.addItem(previousLoc);
            if (count == 0) {
                //board.setInfo("开始测量距离，请单击确定起点");
                info = "";
                this.map.addEventListener(DrawEvent.DRAW_POINT_MOUSEMOVING, mouseMoving);
            }
            else {
                currentLinLength = GMath.TwoLocDistance(previousLoc.reprojectTo(this.map.projection, new Projection("WGS84:TMERC")), points.getItemAt(count - 1).reprojectTo(this.map.projection, new Projection("WGS84:TMERC")));
                //	info=info+"\n"+"第" + count + "条线段距离为：" + currentLinLength + "米";
                totalLength = totalLength + currentLinLength;
                //	info=info + "\n" + "总长:" + totalLength;
                info = ResourceUtil.getString(StringConstants.MEASUREPATHCONTROL_INFO_TOTAL) + totalLength + ResourceUtil.getString(StringConstants.MAP_UNIT_METER);
                board.setInfo(info);
            }

            count++;
        }

        private function drawEndHandler(ev:DrawEvent):void {
            handler.active = false;
            this.map.removeEventListener(DrawEvent.DRAW_NEW_POINT, drawPointHandler);
            this.map.removeEventListener(DrawEvent.DRAW_LINE_FEATURE_END, drawEndHandler)
            this.map.removeEventListener(DrawEvent.DRAW_POINT_MOUSEMOVING, mouseMoving);
            this.map.removeLayer(layer);
            board.setInfo("");
            board.visible = false;

        }

        //画线过程中的移动
        private function mouseMoving(ev:DrawEvent):void {
            var mouseLoc:Location = ev.newPoint;
            currentLinLength = GMath.TwoLocDistance(previousLoc.reprojectTo(this.map.projection, new Projection("WGS84:TMERC")), mouseLoc.reprojectTo(this.map.projection, new Projection("WGS84:TMERC")));
            board.setInfo(ResourceUtil.getString(StringConstants.MEASUREPATHCONTROL_INFO_TOTAL) + totalLength + ResourceUtil.getString(StringConstants.MEASUREPATHCONTROL_INFO_BOARD) + currentLinLength + ResourceUtil.getString(StringConstants.MAP_UNIT_METER));
        }
    }
}